import {Injectable} from '@nestjs/common';
// ORM
import {InjectRepository} from '@nestjs/typeorm';
import {Repository, UpdateResult} from 'typeorm';
import {UserListEntity} from '../entity/user.list.entity';
import {UserListDto} from '../dto/user.list.dto';

@Injectable()
export class UserListService {
    constructor(
        @InjectRepository(UserListEntity)
        private readonly userRepository: Repository<UserListEntity>,
    ) {
    }

    async createOne(userListDto: UserListDto): Promise<UserListEntity> {
        return await this.userRepository.save(userListDto);
    }

    async editOne({id, name, sex, status}): Promise<UpdateResult> {
        return await this.userRepository.update({id}, {name, sex, status});
    }
    async getUserList({status, pageIndex, pageSize}): Promise<[UserListEntity[], number]> {
        const qb = this.userRepository.createQueryBuilder();
        qb.where({
            ...(status && {status}),
        })
        qb.offset((pageIndex - 1) * pageSize)
            .limit(pageSize)
        return qb.getManyAndCount();
    }
}
